//
//  MyPageControl.m
//  
//
//  Created by Mike Chen on 12-7-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyPageControl.h"

@interface MyPageControl ()

@property (nonatomic , strong)UIImage *normalDotImage;
@property (nonatomic , strong)UIImage *highlightedDotImage;
@property (nonatomic , strong)NSMutableArray *dotsArray;
@property (nonatomic , assign)float dotsSize;
@property (nonatomic , assign)NSInteger dotsGap;
@property (nonatomic , retain) UIImageView *highlightedDotImageView;

- (void)dotsDidTouched:(UIView *)sender;

@end

@implementation MyPageControl

/* 
 *pageNum是pageControl的页面总个数
 *size是单个dot的边长
 */
- (id)initWithFrame:(CGRect)frame
        normalImage:(UIImage *)nImage
   highlightedImage:(UIImage *)hImage
         dotsNumber:(NSInteger)pageNum
         sideLength:(NSInteger)size
            dotsGap:(NSInteger)gap
{
    if ((self = [super initWithFrame:frame])) {
        self.userInteractionEnabled = YES;
        self.dotsGap = gap;
        self.dotsSize = size;
        self.dotsArray = [NSMutableArray array];
        _normalDotImage = nImage;
        _highlightedDotImage = hImage;
        self.pageNumbers = pageNum;
        
        CGSize highlightSize = ccs(self.highlightedDotImage.size.width, self.highlightedDotImage.size.height);
        
        UIImageView *dotImageView_h = [[UIImageView alloc] initWithImage:_highlightedDotImage];
        [dotImageView_h.layer setMasksToBounds:NO];
        dotImageView_h.frame = CGRectMake(0, 0,
                                          highlightSize.width,
                                          highlightSize.height);
        self.highlightedDotImageView = dotImageView_h;
        
        for (int i = 0; i != _pageNumbers; ++ i) {
            UIImageView *dotsImageView = [[UIImageView alloc] init];
            dotsImageView.userInteractionEnabled = YES;
            dotsImageView.frame = CGRectMake((size + gap) * i, 0, size, size);
            dotsImageView.tag = 100 + i;
            if (i == 0) {
                CGFloat x = (highlightSize.width - size) / 2;
                self.highlightedDotImageView.frame = CGRectMake((size + gap) * i - x, -x, highlightSize.width, highlightSize.height);
            }
            dotsImageView.image = _normalDotImage;
            [dotsImageView.layer setMasksToBounds:NO];
            
            UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dotsDidTouched:)];
            [dotsImageView addGestureRecognizer:gestureRecognizer];
            [self addSubview:dotsImageView];
        }
        [self addSubview:_highlightedDotImageView];
    }
    return self;
}

- (void)dotsDidTouched:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageControlDidStopAtIndex:)]) {
        [self.delegate pageControlDidStopAtIndex:[[sender view] tag] - 100];
    }
}

- (void)setCurrentPage:(NSInteger)pages animated:(BOOL)animated
{
    if (_normalDotImage || _highlightedDotImage) {
        CGRect newRect = CGRectMake((self.dotsSize + self.dotsGap) * pages, 0,
                                    self.highlightedDotImage.size.width,
                                    self.highlightedDotImage.size.height);
        
        CGFloat offsetX =  (self.highlightedDotImage.size.width - self.normalDotImage.size.width) / 2;
        CGFloat offsetY =  (self.highlightedDotImage.size.height - self.normalDotImage.size.height) / 2;
        newRect.origin.x -= offsetX;
        newRect.origin.y -= offsetY;
        
        if (animated) {
            [UIView animateWithDuration:0.3f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^(void){
                self.highlightedDotImageView.frame = newRect;
            } completion:^(BOOL finished){}];
        } else {
            self.highlightedDotImageView.frame = newRect;
        }
    }
}

@end
